
#include <BUniform>
#include "member_BUniform.h"

using namespace BWE;

#define member					(*(member_BUniform*)_ptr)
#define member_allocate()		_ptr = new member_BUniform(this)
#define member_release()		delete (member_BUniform*)_ptr

BUniform::BUniform(const BString& name, Type type, int count) : BObject(name)
{
	member_allocate();
	member.type = type;
	member.init(type, count);
}
BUniform::~BUniform()
{

}

BUniform::Type BUniform::type() const
{
	return member.type;
}
int BUniform::size() const
{
	return member.size;
}

bool BUniform::set(int value)
{
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		*data = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(float value)
{
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		*data = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(const BVector& vector)
{
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		*data = vector;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(const BColorf& color)
{
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		*data = color;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(const BMatrix& matrix)
{
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		*data = matrix;
		member.dirty = true;
		return true;
	}
	return false;
}

bool BUniform::set(int pos, int value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		data[pos] = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, float value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		data[pos] = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, const BVector& vector)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		data[pos] = vector;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, const BColorf& color)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		data[pos] = color;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, const BMatrix& matrix)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		data[pos] = matrix;
		member.dirty = true;
		return true;
	}
	return false;
}

bool BUniform::get(int& value)
{
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		value = *data;
		return true;
	}
	return false;
}
bool BUniform::get(float& value)
{
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		value = *data;
		return true;
	}
	return false;
}
bool BUniform::get(BVector& vector)
{
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		vector = *data;
		return true;
	}
	return false;
}
bool BUniform::get(BColorf& color)
{
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		color = *data;
		return true;
	}
	return false;
}
bool BUniform::get(BMatrix& matrix)
{
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		matrix = *data;
		member.dirty = true;
		return true;
	}
	return false;
}

bool BUniform::get(int pos, int& value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		value = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, float& value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		value = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, BVector& vector)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		vector = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, BColorf& color)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		color = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, BMatrix& matrix)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		matrix = data[pos];
		return true;
	}
	return false;
}
